package com.witframework.web.tag;


import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class AjaxScrollAreaTag extends BodyTagSupport
{
  private String id = null;
  private String style = "overflow:auto";
  private String className = null;
  private String url = null;
  private int timeout = 0;
  private String direction = "down";
  private String error = null;
  private String beforeAction = null;
  private String afterAction = null;
  private String sBody = null;

  public void setBeforeAction(String paramString)
  {
    this.beforeAction = paramString;
  }

  public String getBeforeAction()
  {
    return this.beforeAction;
  }

  public void setAfterAction(String paramString)
  {
    this.afterAction = paramString;
  }

  public String getAfterAction()
  {
    return this.afterAction;
  }

  public void setId(String paramString)
  {
    this.id = paramString;
  }

  public String getId()
  {
    return this.id;
  }

  public void setStyle(String paramString)
  {
    this.style = paramString;
  }

  public String getStyle()
  {
    return this.style;
  }

  public void setClassName(String paramString)
  {
    this.className = paramString;
  }

  public String getClassName()
  {
    return this.className;
  }

  public void setUrl(String paramString)
  {
    this.url = paramString;
  }

  public String getUrl()
  {
    return this.url;
  }

  public void setTimeout(int paramInt)
  {
    this.timeout = paramInt;
  }

  public int getTimeout()
  {
    return this.timeout;
  }

  public void setDirection(String paramString)
  {
    this.direction = paramString;
  }

  public String getDirection()
  {
    return this.direction;
  }

  public void setError(String paramString)
  {
    this.error = paramString;
  }

  public String getError()
  {
    return this.error;
  }

  public int doAfterBody()
    throws JspException
  {
    BodyContent localBodyContent = getBodyContent();
    if (localBodyContent == null)
      this.sBody = "";
    else
      this.sBody = localBodyContent.getString();
    this.sBody = this.sBody.trim();
    return 0;
  }

  public int doEndTag()
    throws JspException
  {
    StringBuffer localStringBuffer = new StringBuffer("");
    this.timeout = (1000 * this.timeout);
    if (this.sBody == null)
      this.sBody = "";
    localStringBuffer.append("<div id=\"");
    localStringBuffer.append(this.id);
    localStringBuffer.append("\"");
    if (this.className != null)
    {
      localStringBuffer.append(" class=\"");
      localStringBuffer.append(this.className);
      localStringBuffer.append("\"");
    }
    if (this.style != null)
    {
      localStringBuffer.append(" style=\"");
      localStringBuffer.append(this.style);
      localStringBuffer.append("\"");
    }
    localStringBuffer.append(">");
    localStringBuffer.append(this.sBody);
    localStringBuffer.append("</div>\n");
    localStringBuffer.append("<script language=\"JavaScript\" type=\"text/javascript\">\n");
    localStringBuffer.append("var cnt_" + this.id + "=1;");
    localStringBuffer.append("var timer_" + this.id + "=null;");
    String str = "handler_" + this.id;
    localStringBuffer.append("function " + str + "(txt,xmlDoc){");
    if ("up".equals(this.direction))
      localStringBuffer.append("document.getElementById('" + this.id + "').innerHTML=txt+document.getElementById('" + this.id + "').innerHTML;");
    else
      localStringBuffer.append("document.getElementById('" + this.id + "').innerHTML+=txt;");
    localStringBuffer.append("}\n");
    localStringBuffer.append("function update_" + this.id + "(){");
    localStringBuffer.append("var uri='" + this.url + "';");
    localStringBuffer.append("if (uri.indexOf('?')<0) uri+='?';");
    localStringBuffer.append("else uri+='&';");
    localStringBuffer.append("uri+='ajaxcount=';");
    localStringBuffer.append("uri+=cnt_" + this.id + ";");
    localStringBuffer.append("cnt_" + this.id + "++;");
    localStringBuffer.append("if (update_" + this.id + ".arguments.length>0)");
    localStringBuffer.append("{");
    localStringBuffer.append("if (uri.indexOf('?')<0) uri+='?';");
    localStringBuffer.append("else uri+='&';");
    localStringBuffer.append("var cnt=0; while (cnt<update_" + this.id + ".arguments.length) {");
    localStringBuffer.append("if (cnt!=0) uri+='&';");
    localStringBuffer.append("uri+=update_" + this.id + ".arguments[cnt];");
    localStringBuffer.append("cnt++;");
    localStringBuffer.append("if (cnt<update_" + this.id + ".arguments.length) {");
    localStringBuffer.append("uri+='=';");
    localStringBuffer.append("uri+=escape(update_" + this.id + ".arguments[cnt]);");
    localStringBuffer.append("cnt++;");
    localStringBuffer.append("}");
    localStringBuffer.append("}");
    localStringBuffer.append("}");
    localStringBuffer.append("cjAjaxEngine(uri," + str + ",");
    if (this.error == null)
      localStringBuffer.append("null");
    else
      localStringBuffer.append(this.error);
    if (this.beforeAction == null)
      localStringBuffer.append(",null");
    else
      localStringBuffer.append("," + this.beforeAction);
    if (this.afterAction == null)
      localStringBuffer.append(",null");
    else
      localStringBuffer.append("," + this.afterAction);
    localStringBuffer.append(");");
    if (this.timeout > 0)
      localStringBuffer.append("timer_" + this.id + "=setTimeout('update_" + this.id + "()'," + this.timeout + ");");
    localStringBuffer.append("}");
    if (this.timeout > 0)
      localStringBuffer.append("timer_" + this.id + "=setTimeout('update_" + this.id + "()'," + this.timeout + ");");
    localStringBuffer.append("\n</script>\n");
    try
    {
      this.pageContext.getOut().write(localStringBuffer.toString());
    }
    catch (Exception localException)
    {
      throw new JspException("AjaxDiv: could not write data");
    }
    dropData();
    return 6;
  }

  public void release()
  {
    dropData();
  }

  private void dropData()
  {
    this.id = null;
    this.style = "overflow:auto";
    this.className = null;
    this.url = null;
    this.timeout = 0;
    this.direction = "down";
    this.error = null;
    this.beforeAction = null;
    this.afterAction = null;
    this.sBody = null;
  }
}